#ifndef INPUT_FUNCTIONS_H
#define INPUT_FUNCTIONS_H

void processKey(char letter, bool val)
{
	switch(letter)
	{
	case 'd':
	case 'D':
		input.stepRight(val);
		break;
	case 'w':
	case 'W':
		input.stepFoward(val);
		break;
	case 'a':
	case 'A':
		input.stepLeft(val);
		break;
	case 's':
	case 'S':
		input.stepBackward(val);
		break;
    case 27:    // ESC key pressed
        if (val == false)
            input.mouseIsEnabled = !input.mouseIsEnabled;
        break;
	};
}

// watch the keyboard (and the position of the mouse when a key is pressed)
void key(unsigned char letter, int x, int y)
{
	// printf("key:\t %c or %d\n", letter, (int) letter);
	processKey(letter, true);
}

void keyUp(unsigned char letter, int x, int y)
{	
	// printf("keyUp:\t %c\n", letter);
	processKey(letter, false);
}

void processSpecialKey(int code, bool val)
{
	switch(code)
	{
	case 100:
		// input.moveLeft     = val;
		// camera.rotateCameraSpace(10,0,-1,0);
		input.rotateLeft(val);
		break;
	case 101:
		// input.moveFoward   = val;
		// camera.rotateCameraSpace(10,-1,0,0);
		input.rotateUp(val);
		break;
	case 102:
		// input.moveRight    = val;
		// camera.rotateCameraSpace(10,0,1,0);
		input.rotateRight(val);
		break;
	case 103:
		// input.moveBackward = val;
		// camera.rotateCameraSpace(10,1,0,0);
		input.rotateDown(val);
		break;
	};
}

void specialKey(int code, int x, int y) 
{
	// printf("SpecialKey:\t %d\n", code);
	processSpecialKey(code, true);
}

void specialKeyUp(int code, int x, int y)
{
	// printf("SpecialKeyUp:\t %d\n", code);
	processSpecialKey(code, false);
}

// watch the mouse
void mouseMoved(int x, int y) 
{
    if (input.mouseIsEnabled == false)
        return;
	// printf("mouseMoved:\t (%d, %d)\n", x,y);
    input.mouseX = x;
	input.mouseY = y;

    // if mouse is not in center of screen
    if (!input.mouseWasCentered)
    {
        int angleX = (x - input.prevMouseX) / 5;
        int angleY = (input.prevMouseY - y) / 5;
        camera.rotateCameraSpace(angleX,  0, 1, 0);
        camera.rotateCameraSpace(angleY, -1, 0, 0);
        input.centerMouseCoords();
    }
    else
    {
        input.prevMouseX = input.centerW;
        input.prevMouseY = input.centerH;
        input.mouseWasCentered = false;
    }
}

void mouseDragged(int x, int y) 
{
    if (input.mouseIsEnabled == false)
        return;
	//printf("mouseDragged:\t (%d, %d)\n", x,y);
	//mouseMoved(x,y);
}

void mouseButton(int button, int state, int x, int y) 
{
    if (input.mouseIsEnabled == false)
        return;
	//printf("mouseButton:\n\t\tbutton:\t%d\n\t\tstate:\t%d\n\t\tposition:\t(%d, %d)\n", button,state,x,y);

	bool pressed = state == 0; 
	switch(button)
	{
	case 0:        // Left Click
		input.mouseLeftButtonOn = pressed;
		break;
	case 1:        // Middle Click
		input.mouseMiddleButtonOn = pressed;
		break;
	case 2:        // Right Click
		input.mouseRightButtonOn = pressed;
		break;
	};

	// if state is 0 then user is holding button down
	//    otherwise if state is 1 then user released the button
	input.mouseIsDragging = (state == 0);
}



#endif